#include <GL/glut.h>
#include "player.h"
#include "../view/draw.h"


void Player::setCurrents()
{
	if(islandIds.size()>0)
		currentIsland=0;
	else
		currentIsland=-1;

	if(ships.size()>0)
		currentShip=0;
	else
		currentShip=-1;
}

bool Player::isStatic()
{
	//TODO Check Missiles Whether Static or Not

	//Check Ships
	for(unsigned int i=0;i<ships.size();i++)
		if(!ships[i].isStatic())
			return false;
	return true;
}

void Player::pushBattleShip(BattleShip ship)
{
	ships.push_back(ship);
}


pair<float,float> Player::getCurrentShipsPosition()
{
	return ships[currentShip].getInitialPosition();
}

float Player::getCurrentShipsOrientation()
{
	return ships[currentShip].getInitialOrientation();
}

void Player::selectNextShip()
{
	++currentShip%=ships.size();
}

void Player::calculateCurrentShipsPosition()
{
	ships[currentShip].calculatePosition();
}

void Player::moveCurrentShip(int mode)
{
	ships[currentShip].move(mode);
}

void Player::setCurrentShip(int shipId)
{
	currentShip=shipId;
}

void Player::drawInfo(float x,int id)
{
	glColor3f(0,0,0);
	if(id==1)
		Draw::Text(x+20,575,"Player 1",-1,BIG);
	else if(id==2)
		Draw::Text(x+20,575,"Player 2",-1,BIG);

	Draw::Mine(x+30,500);
	Draw::Missile(x+30,450,0);
	Draw::Missile(x+30,400,1);
	Draw::Missile(x+30,350,2);
}

void Player::draw(int myTurn,float seaLevel,int mode,int isDay)
{
	//Draw ships by gulsah
	for(unsigned int i=0;i<ships.size();i++)
	{
		glPushName(i);
		if(mode==BATTLESHIP)
		{
			if(i%2)
			{
				if(currentShip==(int)i && myTurn)
					ships[i].draw3d(seaLevel,1,isDay);
				else
					ships[i].draw3d(seaLevel,0,isDay);
			}
			else
			{
				if(currentShip==(int)i && myTurn)
					ships[i].drawTradeShip(seaLevel,1,isDay);
				else
					ships[i].drawTradeShip(seaLevel,0,isDay);
			}
		}
		else if(mode==MISSILE)
		{
			if(currentShip==(int)i && myTurn)
				ships[i].drawMissile(seaLevel,1);
			else
				ships[i].drawMissile(seaLevel,0);
		}
		else if(mode==MINE)
		{
			if(currentShip==(int)i && myTurn)
				ships[i].drawMine(seaLevel,1);
			else
				ships[i].drawMine(seaLevel,0);
		}
		glPopName();
	}
	glPopName();// Pop the name pushed when calling THIS draw function.
	//if(port.getPlayerId()==1) port.draw3d(seaLevel);
	if(port.getPlayerId()!=-1) port.draw3d(seaLevel);
}


